/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.base.model.FinancialProject;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.base.db.dao.FinancialProjectDao;
import com.yunhoo.base.model.Function;
import com.yunhoo.base.model.Person;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import com.yunhoo.sys.util.common.ToolKit;
import com.yunhoo.sys.util.tree.TreeUtil;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.richfaces.event.TreeSelectionChangeEvent;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "finProjBean")
@SessionScoped
public class FinancialProjectBean implements Serializable {

    private static DaoManager daom;
    private static FinancialProjectDao dao;
    //
    private FinancialProject finProj = null;
    private DataModel<FinancialProject> finModel = null;
    private boolean addOrUpdate = false;
    private boolean editStatus = false;
    //
    private String finNameFilter = "";

    public FinancialProjectBean() {
        daom = DaoConfig.getDaoManager();
        dao = (FinancialProjectDao) daom.getDao(FinancialProjectDao.class);
    }

    private void initList() {
        if (finModel == null) {
            finModel = new ListDataModel<FinancialProject>();
        }
        try {
            List<FinancialProject> finli = dao.queryAll(new FinancialProject(true));
            finModel.setWrappedData(finli);
        } catch (SQLException ex) {
            Logger.getLogger(FinancialProjectBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void initStatus() {
        addOrUpdate = false;
        editStatus = false;
    }

    public void funTreeSelected(TreeSelectionChangeEvent event) {
        Function temp = (Function) TreeUtil.listener(event);
        if (temp.getIsEnd() == 1) {
            finProj.setFunCode(temp.getCode());
            finProj.setFunName(temp.getName());
        }
    }

    public void personSelected(Person person) {
        finProj.setManagerCode(person.getCpsn_number());
        finProj.setManagerName(person.getCpsn_name());
    }

    public void add() {
        editStatus = true;
        addOrUpdate = true;
        finProj = new FinancialProject(true);
        finProj.setFinCode(maxCode());
    }

    private String maxCode() {
        Integer maxCode = dao.maxCode();
        if (maxCode == null) {
            maxCode = 0;
        }
        return String.format("%04d%n", maxCode + 1).trim();
    }

    public void update() {
        addOrUpdate = false;
        editStatus = true;
    }

    public void delete() {
        boolean ok = true;
        try {
            dao.delete(finProj);
        } catch (SQLException ex) {
            ok = false;
            Logger.getLogger(PersonBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ok) {
            finProj = null;
            initStatus();
            initList();
        }
    }

    public void save() {
        boolean ok = true;
        try {
            if (addOrUpdate) {
                finProj.setJl_RQ(new Date());
                finProj.setJlr_ID(LoginParam.getPer().getCpsn_number());
                dao.insert(finProj);
            } else {
                finProj.setXg_RQ(new Date());
                finProj.setXgr_ID(LoginParam.getPer().getCpsn_number());
                dao.update(finProj);
            }
        } catch (SQLException ex) {
            ok = false;
            Logger.getLogger(PersonBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ok) {
            initStatus();
            initList();
        }
    }
    public void checkSave(){
        if(!Null.check(finProj.getCostType()) && finProj.getCostType() == 1 && Null.check(finProj.getBudgetCode())){
            ToolKit.msgs_err("不能为空！");
        }else{
            save();
        }
    }

    public void cancel() {
        if (addOrUpdate) {
            finProj = null;
        }
        initStatus();
    }

    public void selected() {
        finProj = finModel.getRowData();
        initStatus();
    }

    public FinancialProject getFinProj() {
        return finProj;
    }

    public void setFinProj(FinancialProject finProj) {
        this.finProj = finProj;
    }

    public DataModel<FinancialProject> getFinModel() {
        if (finModel == null) {
            initList();
        }
        return finModel;
    }

    public void setFinModel(DataModel<FinancialProject> finModel) {
        this.finModel = finModel;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }

    public String getFinNameFilter() {
        return finNameFilter;
    }

    public void setFinNameFilter(String finNameFilter) {
        this.finNameFilter = finNameFilter;
    }
}
